<template>
  <div class="app-container">
    <div class="head-container">
      <el-button
          v-waves
          class="filter-item"
          style="margin-left: 10px;"
          type="primary"
          icon="edit"
          @click="handleCreate"
      >添加
      </el-button>
    </div>

    <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        style="width: 100%;"
    >
      <el-table-column align="center" label="ID" width="95">
        <template v-slot="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="分类名">
        <template v-slot="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="排序" width="110" align="center">
        <template v-slot="scope">
          <span>{{ scope.row.sortId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template v-slot="{row}">
          <el-button type="primary" size="small" @click="handleUpdate(row)">编辑</el-button>
          <el-button
              v-if="row.status!='deleted'"
              size="small"
              type="danger"
              @click="handleDelete(row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

      <el-dialog draggable :title="textMap[dialogStatus]" v-model="dialogFormVisible">
        <el-form
            ref="dataForm"
            :rules="rules"
            :model="temp"
            label-position="left"
            label-width="70px"
            style="width: 400px; margin-left:50px;"
        >
          <el-form-item label="名称" prop="name">
            <el-input v-model="temp.name"/>
          </el-form-item>
          <el-form-item label="排序" prop="sortId">
            <el-input-number v-model="temp.sortId" label="描述文字"/>
          </el-form-item>
        </el-form>
        <template #footer class="dialog-footer">
          <el-button @click="dialogFormVisible = false">关闭</el-button>
          <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
        </template>
      </el-dialog>
  </div>
</template>

<script>
import {getList, create, remove, update} from '@/api/articlecat'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      temp: {
        id: undefined,
        sortId: 0,
        name: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '添加'
      },
      rules: {
        sortId: [
          {
            type: 'number',
            required: true,
            message: '请输入正确的数字',
            trigger: 'change'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList().then(data => {
        this.list = data
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        sortId: 0,
        title: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          create(this.temp).then(() => {
            this.fetchData()
            this.dialogFormVisible = false
            this.$message({
              title: '成功',
              message: '分类添加成功',
              type: 'success'
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          update(tempData).then(() => {
            this.fetchData()
            this.dialogFormVisible = false
            this.$message({
              title: '成功',
              message: '分类修改成功',
              type: 'success'
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作不能被还原, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
      })
    }
  }
}
</script>
